Repository navigation
feat: add Trainer control-plane metrics - #4190
abhijeet-dhumal wants to merge 4 commits into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
robert-bell
left a comment
There was a problem hiding this comment.
Thanks @abhijeet-dhumal! I've left some inline feedback.
71adb35 to
9b58b90
Compare
|
Thanks @robert-bell .. I’ve tried to address the metric scope and test coverage updates. |
683d2ac to
c2a4081
Compare
1ad4f92 to
0f47c2d
Compare
andreyvelich
left a comment
There was a problem hiding this comment.
Thanks @abhijeet-dhumal for the work on this! Review done using AI tools; inline comments below.
Signed-off-by: abhijeet-dhumal <abhijeetdhumal652@gmail.com>
db98949 to
0bebcf4
Compare
|
/milestone v2.4 |
robert-bell
left a comment
There was a problem hiding this comment.
Thanks @abhijeet-dhumal this is looking pretty close!
| }, []string{"result", "runtime_ref"}) | ||
| trainJobRequestedTrainingNodes = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Namespace: metricNamespace, | ||
| Name: "trainjob_requested_training_nodes_per_job", |
There was a problem hiding this comment.
We don't need to add this in the phase 1, right @robert-bell @abhijeet-dhumal ?
| Name: "trainjob_requested_training_nodes_per_job", |
There was a problem hiding this comment.
I'd be keen to keep this in phase 1. It gives admins info about job sizes which is useful for investing in optimising network configs.
| log: ctrl.Log.WithName("trainer-metrics"), | ||
| runtimeInfo: newRuntimeInfoCollector(c), | ||
| } |
There was a problem hiding this comment.
Why do we need a separate informer handler @abhijeet-dhumal @robert-bell?
In JobSet, we directly call the metrics counter from the controller loop: https://github.com/kubernetes-sigs/jobset/blob/main/pkg/controllers/failure_policy.go#L432
There was a problem hiding this comment.
Using the informer completely decouples the metrics instrumentation from the reconciliation logic keeping both clean and easier to evolve independently.
There was a problem hiding this comment.
Do we prefer approach to decouple it?
Would love to hear feedback from the Kueue maintainer on this too @kannon92 @tenzen-y!
https://github.com/kubernetes-sigs/kueue/tree/main/pkg/metrics
There was a problem hiding this comment.
I prefer keeping the informer-based observer... it keeps metrics independent of reconciliation and records lifecycle events without adding metrics work to the reconcile path.
There was a problem hiding this comment.
@abhijeet-dhumal we talked about this today at the Trainer call with @robert-bell and @tenzen-y.
Ref: https://youtu.be/nsE0a4T9LFM?t=959
Given the trade-offs of using separate watchers for events, could we explore how other Kubernetes projects handle event processing, particularly since our current implementation differs from Kueue and Training Operator v1?
My main concern with your proposed approach is that metric updates would rely solely on eventual consistency, as the metrics might not be updated at the time the corresponding event is processed.
There was a problem hiding this comment.
Hey @andreyvelich, @tenzen-y, I've done an investigation into this.
I looked back at the call and I think there were 2 points we wanted to clarify:
- What's the existing precedent for using the informer-cache pattern, rather than instrumenting the reconcile loop like kueue and Training Operator v1?
- Are there any problems with the eventual consistency of the informer-backed metrics? Yuki's concern was that metrics recorded at cache-event time rather than controller-action time would diverge from the controller logs, making it harder for admins to correlate the two when debugging production issues.
How other projects do it.
The pattern in this PR has more precedent than the Kueue/Training Operator v1 comparison suggests:
- Kueue records
finished_workloads_totalfrom a watch event handler on the shared informer cache, with old/new transition guards (workload_controller.go, handleUpdate) -- structurally the same approach as this PR's terminal counters. Its scheduler-decision metrics are recorded at the decision point, which is the other category. - Several widely-deployed projects derive object-state metrics from informer-backed caches, decoupled from reconcile loops:
- kube-state-metrics: reflectors feed watch events into a
MetricsStoreserved on scrape (internal/store/builder.go) - cert-manager:
prometheus.Collectors over informer-backed listers (internal/collectors/certificate_collector.go); migrated to this pattern in v1.19 to fix stale-series bugs in its controller-instrumented metrics (#7301) - KubeVirt: collect-callback reads the VMI informer store at scrape time (vmistats_collector.go)
- Argo Rollouts: lister-backed
argo_rollouts_infocollectors alongside reconcile counters in the same registry (controller/metrics/metrics.go) - KubeRay:
Collect()lists RayJob/RayCluster objects through the manager's cached client (ray_job_metrics.go) - prometheus-adapter: metadata informers/listers back its entire serving path (cmd/adapter/adapter.go)
- kube-state-metrics: reflectors feed watch events into a
- JobSet and Training Operator v1 do count in the reconcile loop, and JobSet's own code comments document working around retry-driven "metric inflation" in that design.
The distinction that falls out is state-derived vs action-derived metrics. All the metrics in this PR are state metrics (what exists / what happened in the cluster), which is what the informer pattern is for; controller-behaviour metrics are already exported by controller-runtime's built-in reconcile_* family.
Eventual consistency in practice.
This PR is using the same cache as the main reconciliation loop, so there's no realistic divergence from what the reconcile loop would see. I did measure latency locally on a kind cluster which confirms the delay is minimal - e.g. trainjob_failed_total incremented within ~14 ms of an independent watch stream observing the terminal condition -- in some trials before my own watcher received the event.
@tenzen-y on your log-correlation concern: counter samples are timestamped at scrape time, so no user can localize a counter increment finer than the scrape interval (e.g. 30 s default) regardless of where the increment happens -- moving it into the reconcile loop would not change observable timing.
Recommendation
I think we should be good to go ahead with the implementation as-is -- though we can add an alpha feature gate for it if you'd prefer.
We do need to update docs/operator-guides/metrics.md to mention the metric collections are best effort. @abhijeet-dhumal could you add that to this PR when you get a chance? I'll add some suggestions.
For transparency, I used AI to help me with the investigation, but I've manually reviewed and edited the information.
Signed-off-by: abhijeet-dhumal <abhijeetdhumal652@gmail.com>
Signed-off-by: abhijeet-dhumal <abhijeetdhumal652@gmail.com>
Signed-off-by: abhijeet-dhumal <abhijeetdhumal652@gmail.com>
|
@andreyvelich I’ve tried to address the review comments in the latest changes, including the documentation updates. Could you please take another look when you have a chance and let me know if anything else needs work? |
|
/milestone clear |
|
Need to discuss/align and sign off on structural LLD for this implementation! |
| - Creation, completion, and workload metrics are event-based and reset when | ||
| the controller process restarts or leadership changes. Existing objects | ||
| found during the initial informer listing are not counted as newly created. |
There was a problem hiding this comment.
I think we could add this clarification to help users understand the limitation -
| - Creation, completion, and workload metrics are event-based and reset when | |
| the controller process restarts or leadership changes. Existing objects | |
| found during the initial informer listing are not counted as newly created. | |
| - Creation, completion, and workload metrics are event-based and reset when | |
| the controller process restarts or leadership changes. Existing objects | |
| found during the initial informer listing are not counted as newly created. | |
| As a consequence, TrainJobs created while no leader replica is running | |
| (for example, during a leadership transition or controller downtime) are | |
| not counted by `kubeflow_trainer_trainjob_created_total`. |
| - Labels use normalized identifiers and bounded categories, but total | ||
| cardinality still grows with the number of runtimes and namespaces. Avoid | ||
| adding job names, UIDs, raw errors, or other unbounded values. | ||
|
|
There was a problem hiding this comment.
Suggest we add this clarification
| - The TrainJob metrics are updated by event handlers on the same informer cache | |
| that drives reconciliation, so they are eventually consistent with the API | |
| server: increments appear shortly after the corresponding object change, | |
| with a delay that is small relative to a typical scrape interval. | |
What this PR does / why we need it
Adds six upstream Trainer Prometheus metrics for runtime inventory, TrainJob creation, completion, failure, lifetime, and requested training-node counts proposed in #3429:
Metrics are registered only by the elected controller leader and collected from informer-backed snapshots without Kubernetes API calls during scraping. The implementation also handles duplicate terminal events, runtime updates/deletions, and leader transitions.
TLS, authentication, and ServiceMonitor configuration are handled separately by PR #4102.
kubeflow_trainer_runtime_info: current TrainingRuntime and ClusterTrainingRuntime inventory.kubeflow_trainer_trainjob_created_total: TrainJobs observed after the initial informer cache listing.kubeflow_trainer_trainjob_finished_total: terminal outcomes by result, reason, and runtime.kubeflow_trainer_trainjob_lifetime_seconds: time from TrainJob creation to terminal state.kubeflow_trainer_trainjob_requested_training_nodes_per_job: requested training-node size.The controller uses its existing informer caches instead of calling the Kubernetes API during metric collection. Runtime information is kept in an in-memory snapshot and reused when processing TrainJobs.
Only the elected controller leader processes metric events and registers the Trainer metrics. When leadership changes, the old leader removes its metrics and the new leader registers them. This prevents duplicate metrics from multiple controller replicas.
The implementation also:
Validation
go vet ./pkg/metrics/...git diff --check.Follow-up
Final CI, documentation build, approver review, and cluster E2E validation remain required before merge.
Checklist